package com.jzsm.config;


import com.jzsm.common.utils.ConstantsUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Value("${jwt.tokenName}")
    private String TOKEN_NAME;

    @Bean
    public Docket createRestApi(Environment environment) {
        Supplier<ParameterBuilder> parameterBuilderCopier = () -> new ParameterBuilder();

        List<Parameter> par = new ArrayList<>();

        // query 参数
//        par.add(parameterBuilderCopier.get().name(PageDomain.PAGE_NUM).description("分页参数-当前记录起始索引").modelRef(new ModelRef("integer")).parameterType("query").required(false).build());

        return new Docket(DocumentationType.SWAGGER_2)
                // 开发环境开启
                .enable(
                        environment.acceptsProfiles(Profiles.of("dev")) ||
                                environment.acceptsProfiles(Profiles.of("test"))
                )
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.scm.app"))
                .paths(PathSelectors.any())
                .build().globalOperationParameters(par)
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }

    private List<ApiKey> securitySchemes() {
        List<ApiKey> apiKeys = new ArrayList<>();
        apiKeys.add(new ApiKey(TOKEN_NAME, TOKEN_NAME, "header"));
        apiKeys.add(new ApiKey(ConstantsUtils.GROUP_ID_KEY, ConstantsUtils.GROUP_ID_KEY, "query"));
        return apiKeys;
    }

    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("^(?!auth).*$"))
                        .build());
        return securityContexts;
    }

    /**
     * 处理 公共参数
     *
     * @return
     */
    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference(TOKEN_NAME, authorizationScopes));
        securityReferences.add(new SecurityReference(ConstantsUtils.GROUP_ID_KEY, authorizationScopes));
        return securityReferences;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档")
                .version("1.0")
                .build();
    }
}
